/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import bllobjects.Account;
import java.util.ArrayList;
import java.util.Collection;
import javax.swing.AbstractListModel;

/**
 *
 * @author Mrkupi
 */
public class FriendListModel extends AbstractListModel
{
    private ArrayList<Account> fullAccounts;
    private ArrayList<Integer> searchIndices;
    private String searchString;
    
    public FriendListModel()
    {
        fullAccounts = new ArrayList();
        searchIndices = new ArrayList();
        this.searchString = "";
    }

    @Override
    public int getSize() 
    {
        return searchIndices.size();
    }

    @Override
    public Object getElementAt(int index) {
        return this.getFullAccounts().get(searchIndices.get(index));
    }
    
    public void addAccount( Account acc )
    {
        this.getFullAccounts().add(acc);
        
        if (this.testAccount(acc))
        {
            this.searchIndices.add(this.getFullAccounts().size() - 1);
            this.fireIntervalAdded(this, this.searchIndices.size() - 1, this.searchIndices.size());
        }
    }
    
    public void addAccounts(Collection<Account> accs)
    {
        for (Account acc : accs)
        {
            this.addAccount(acc);
        }
    }
    
    public void removeAccount(Account acc)
    {
        for (int i = 0; i < this.fullAccounts.size(); ++i)
        {
            if (acc.getUserID().equals(this.fullAccounts.get(i).getUserID()))
            {
                this.fullAccounts.remove(i);
                if (this.searchIndices.contains(i))
                {
                    this.searchIndices.remove((Integer) i);
                    this.fireIntervalRemoved(this, i, i);
                }
            }
        }
    }
    
    public void search( String name )
    {
        this.setSearchString(name);
        for (int i = 0; i < this.getFullAccounts().size(); ++i)
        {
            if (this.testAccount(this.getFullAccounts().get(i)))
            {
                this.searchIndices.add(i);
            }
        }
        
        this.fireContentsChanged(this, 0, this.searchIndices.size());
    }
    
    public void clear()
    {
        int lastSize = this.fullAccounts.size();
        this.fullAccounts.clear();
        this.searchIndices.clear();
        
        this.fireContentsChanged(this, 0, lastSize);
    }
    
    private boolean testAccount(Account acc)
    {
        return ("".equals(getSearchString()) || acc.getUserName().contains(getSearchString()));
    }

    /**
     * @return the fullAccounts
     */
    public ArrayList<Account> getFullAccounts() {
        return fullAccounts;
    }

    /**
     * @return the searchString
     */
    public String getSearchString() {
        return searchString;
    }

    /**
     * @param searchString the searchString to set
     */
    public void setSearchString(String searchString) {
        this.searchString = searchString;
    }
}
